package com.glory.good_dark.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.CheckTableListAdapter;
import com.glory.good_dark.adapter.RecipeNameListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.RecipeInfoEntity;
import com.glory.good_dark.entity.UnlockEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.MyUtils;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * FileName: RecipeInfoActivity
 * Author: XJH
 * Date: 2020/4/20 14:48
 * Description: Recipe下载
 * History:
 */
public class RecipeInfoActivity extends BaseProjectActivity {
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.rvRecipeInfo)
    RecyclerView rvRecipeInfo;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etRecipeName)
    EditText etRecipeName;
    @BindView(R.id.etLot)
    EditText etLot;
    private RecipeNameListAdapter recipeNameListAdapter;
    private RecipeInfoEntity recipeInfoEntity;
    private List<RecipeInfoEntity.RecipeName> recipeNameList;

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_info;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        initRecyclerView(rvRecipeInfo,this);
        recipeNameListAdapter = new RecipeNameListAdapter(null);
        recipeNameListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recipeNameListAdapter.isFirstOnly(true);
        rvRecipeInfo.setAdapter(recipeNameListAdapter);

        MyUtils.setEditTextListener(etLot, this, new FreeUI_TextCallback() {
            @Override
            public void onSuccess(String s) {
                requestData(s);
            }
        });

        etRecipeName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!etLot.getText().toString().equals("")){
                    searchRecipe(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        recipeNameListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                etRecipeName.setText(recipeNameListAdapter.getData().get(position).getRECIPE_NAME());
            }
        });
    }

    private void searchRecipe(String condition) {
        if (recipeNameList != null && recipeNameList.size() > 0) {
            if (condition.trim().length() > 0) {
                List<RecipeInfoEntity.RecipeName> searchList = new ArrayList<>();
                for (RecipeInfoEntity.RecipeName recipeName : recipeNameList) {
                    if (recipeName.getRECIPE_NAME().contains(condition.toUpperCase()) ||
                            recipeName.getRECIPE_NAME().contains(condition)) {
                        searchList.add(recipeName);
                    }
                }
                recipeNameListAdapter.setNewData(searchList);
            } else {
                recipeNameListAdapter.setNewData(recipeNameList);

            }
        }

    }

    private void requestData(String s){
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDARecipeInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"PROCESS\":\"GX001\",\n" +
//                "            \"RECIPE_NAME_LIST\":[\n" +
//                "                {\n" +
//                "                    \"RECIPE_NAME\":\"4KUAISU1\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"RECIPE_NAME\":\"55UAISU1\"\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"RETURN\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<RecipeInfoEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<RecipeInfoEntity>>() {}.getType());
//        recipeInfoEntity = bean.getData().getBody();
//        recipeNameList = recipeInfoEntity.getRECIPE_NAME_LIST();
//        if (recipeNameList.size() > 0){
//            recipeNameListAdapter.setNewData(recipeNameList);
//        }else {
//            showToast("未获取到recipe信息",false);
//        }
        FreeUI_EntityCallBack<BaseEntity<RecipeInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<RecipeInfoEntity>>
                (new TypeToken<BaseEntity<RecipeInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<RecipeInfoEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    recipeInfoEntity = response.body().getData().getBody();
                    if (recipeInfoEntity.getRECIPE_NAME_LIST().size() > 0){
                        recipeNameListAdapter.setNewData(recipeInfoEntity.getRECIPE_NAME_LIST());
                    }else {
                        showToast("未获取到recipe信息",false);
                    }
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<RecipeInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDARecipeInfo");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID",App.EQPID);
        jsonObject.addProperty("LOT_ID",s);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDARecipeInfo", jsonObject);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Recipe下载");
        freeUI_titleView.setRightListener("开始下载", v -> {
//            if(recipeInfoEntity.getPROCESS().equals("Q040")){
//                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "是否调整轨道", new FreeUI_CommonCallback() {
//                    @Override
//                    public void onSuccess() {
//                        submit(1);
//                    }
//                }, new FreeUI_CommonCallback() {
//                    @Override
//                    public void onSuccess() {
//                        submit(0);
//                    }
//                }).show(getSupportFragmentManager(),"");
//            }else {
//                submit(2);
//            }
            if (etRecipeName.getText().toString().equals("")){
                showToast("请选择Recipe名称",false);
                return;
            }
            submit(2);
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void submit(int adjustTrack) {
        FreeUI_EntityCallBack<BaseEntity<JsonObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JsonObject>>
                (new TypeToken<BaseEntity<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<JsonObject>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    showConfirmDialog(RecipeInfoActivity.this,response.body().getData().getReturn().getRETURNMESSAGE());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<JsonObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PDARecipe");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID",App.EQPID);
        jsonObject.addProperty("LOT_ID",etLot.getText().toString());
//        jsonObject.addProperty("ADJUST_TRACK",adjustTrack);
        jsonObject.addProperty("RECIPE_NAME",etRecipeName.getText().toString());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDARecipe", jsonObject);
    }
}
